home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / incl / LEDA.020+881 / segment.h < prev    next >
C/C++ Source or Header  |  1994-11-09  |  4KB  |  134 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  segment.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_SEGMENT_H
  16. #define LEDA_SEGMENT_H
  17.  
  18. #include <LEDA/point.h>
  19.  
  20. //------------------------------------------------------------------------------
  21. // segments
  22. //------------------------------------------------------------------------------
  23.  
  24.  
  25. class segment_rep : public handle_rep {
  26.  
  27. friend class segment;
  28. friend class line;
  29. friend class circle;
  30.    
  31.    point start;
  32.    point end;
  33.  
  34.    double slope;
  35.    double y_abs;
  36.    double angle;
  37.  
  38. public:
  39.    
  40.    segment_rep(const point&, const point&);
  41.    segment_rep();  
  42.  
  43.   ~segment_rep() {}
  44.  
  45.    
  46.    LEDA_MEMORY(segment_rep)
  47.    
  48. };
  49.  
  50.  
  51. class segment  : public handle_base 
  52. {
  53.  
  54. friend class line;
  55. friend class circle;
  56.  
  57. segment_rep* ptr() const { return (segment_rep*)PTR; }
  58.  
  59. public:
  60.  
  61.  segment();                 
  62.  segment(const point&, const point&); 
  63.  segment(double, double, double, double) ;
  64.  segment(const point&, double dir, double length);
  65.  segment(const segment& s) : handle_base(s) {}     
  66. ~segment()                { clear(); }
  67.  
  68. segment& operator=(const segment& s) { handle_base::operator=(s); return *this;}
  69.  
  70. operator vector()  { return vector(xcoord2()-xcoord1(), ycoord2()-ycoord1()); }
  71.  
  72. bool intersection(const segment& s, point& inter) const;
  73.  
  74. point start()  const      { return ptr()->start; }
  75. point end()    const      { return ptr()->end; }
  76.  
  77. double xcoord1() const    { return ptr()->start.ptr()->x; }
  78. double xcoord2() const    { return ptr()->end.ptr()->x;   }
  79. double ycoord1() const    { return ptr()->start.ptr()->y; }
  80. double ycoord2() const    { return ptr()->end.ptr()->y;   }
  81.  
  82. double slope() const { return ptr()->slope; }
  83. double y_abs() const { return ptr()->y_abs; }
  84.  
  85. double angle()     const { return ptr()->angle; }
  86. double direction() const { return angle(); }
  87.  
  88. bool vertical()   const { return xcoord1() == xcoord2(); }
  89. bool horizontal() const { return ycoord1() == ycoord2(); }
  90.  
  91. double length() const { return start().distance(end()); }
  92.  
  93. segment translate(double,double) const;
  94. segment translate(const vector&) const;
  95.  
  96. double  angle(const segment&) const;
  97.  
  98. double  distance(const segment&) const;
  99. double  distance(const point&) const;
  100. double  distance() const;
  101. double  x_proj(double) const;
  102. double  y_proj(double) const;
  103.  
  104. double operator()(double x) { return y_proj(x); }
  105.  
  106. segment rotate(const point&,double) const;
  107. segment rotate(double) const;
  108.  
  109. bool  right()  const     { return ptr()->start.ptr()->x < ptr()->end.ptr()->x; }
  110. bool  left()   const     { return ptr()->start.ptr()->x > ptr()->end.ptr()->x; }
  111. bool  up()     const     { return ptr()->start.ptr()->y < ptr()->end.ptr()->y; }
  112. bool  down()   const     { return ptr()->start.ptr()->y > ptr()->end.ptr()->y; }
  113.  
  114. segment operator+(const vector& v) const { return translate(v); }
  115.  
  116. int operator==(const segment& s) const
  117. { return (ptr()->start == s.ptr()->start && ptr()->end == s.ptr()->end); }
  118.  
  119. int operator!=(const segment& s) const { return !operator==(s);}
  120.  
  121. friend ostream& operator<<(ostream& out, const segment& s);
  122. friend istream& operator>>(istream& in, segment& s);
  123.  
  124. };
  125.  
  126. inline void Print(const segment& s, ostream& out) { out << s; } 
  127. inline void Read(segment& s,  istream& in)        { in >> s; }
  128.  
  129. LEDA_HANDLE_TYPE(segment)
  130.  
  131.  
  132. #endif
  133.